package de.elmar_baumann.nb.slclock.alarmclock;

import java.util.Arrays;
import java.util.Collection;
import org.openide.util.NbPreferences;

/**
 * @author Elmar Baumann
 */
public final class AlarmEventSnoozeTimePanel extends javax.swing.JPanel {

    private static final String KEY_SNOOZE_TIME = "AlarmEventSnoozeTimePanel.SnoozeTime";
    private static final Collection<Integer> SNOOZE_TIMES = Arrays.asList(0, 1, 5, 10, 15, 30, 45, 60);
    private static final long serialVersionUID = 1L;

    public AlarmEventSnoozeTimePanel() {
        this("Event without name");
    }

    public AlarmEventSnoozeTimePanel(String eventDisplayName) {
        if (eventDisplayName == null) {
            throw new NullPointerException("eventDisplayName == null");
        }
        initComponents();
        setEventDisplayName(eventDisplayName);
        comboBoxSnooze.setSelectedItem(NbPreferences.forModule(AlarmEventSnoozeTimePanel.class).getInt(KEY_SNOOZE_TIME, 5));
    }

    public void setEventDisplayName(String displayName) {
        if (displayName == null) {
            throw new NullPointerException("displayName == null");
        }
        labelEventDisplayName.setText(displayName);
    }

    public String getEventDisplayName() {
        return labelEventDisplayName.getText();
    }

    public void setSnoozeTimeInMinutes(int minutes) {
        comboBoxSnooze.setSelectedItem(minutes);
    }

    public int getSnoozeTimeInMinutes() {
        return (int) comboBoxSnooze.getSelectedItem();
    }

    public boolean isSnoozeTime(int time) {
        return SNOOZE_TIMES.contains(time);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        labelEventDisplayName = new javax.swing.JLabel();
        labelSnoozePrompt = new javax.swing.JLabel();
        comboBoxSnooze = new javax.swing.JComboBox<>();
        labelSnoozeUnits = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(labelSnoozePrompt, org.openide.util.NbBundle.getMessage(AlarmEventSnoozeTimePanel.class, "AlarmEventSnoozeTimePanel.labelSnoozePrompt.text")); // NOI18N

        comboBoxSnooze.setModel(new javax.swing.DefaultComboBoxModel<>(SNOOZE_TIMES.toArray(new Integer[SNOOZE_TIMES.size()])));
        comboBoxSnooze.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxSnoozeActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(labelSnoozeUnits, org.openide.util.NbBundle.getMessage(AlarmEventSnoozeTimePanel.class, "AlarmEventSnoozeTimePanel.labelSnoozeUnits.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelSnoozePrompt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBoxSnooze, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelSnoozeUnits))
                    .addComponent(labelEventDisplayName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelEventDisplayName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSnoozePrompt)
                    .addComponent(comboBoxSnooze, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSnoozeUnits))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }//GEN-END:initComponents

    private void comboBoxSnoozeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxSnoozeActionPerformed
        NbPreferences.forModule(AlarmEventSnoozeTimePanel.class).putInt(KEY_SNOOZE_TIME, getSnoozeTimeInMinutes());
    }//GEN-LAST:event_comboBoxSnoozeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<Integer> comboBoxSnooze;
    private javax.swing.JLabel labelEventDisplayName;
    private javax.swing.JLabel labelSnoozePrompt;
    private javax.swing.JLabel labelSnoozeUnits;
    // End of variables declaration//GEN-END:variables

}
